package com.yq.cp.service.yw.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.entity.yw.YwZbgl;
import com.yq.cp.entity.yw.YwZbglGlry;
import com.yq.cp.entity.yw.YwZbglXq;
import com.yq.cp.mapper.yw.YwZbglGlryMapper;
import com.yq.cp.mapper.yw.YwZbglMapper;
import com.yq.cp.mapper.yw.YwZbglXqMapper;
import com.yq.cp.service.yw.YwZbglXqService;
import com.yq.cp.utils.DateTimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运维-值班管理-详情 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@Service
@Transactional
public class YwZbglXqServiceImpl extends ServiceImpl<YwZbglXqMapper, YwZbglXq> implements YwZbglXqService {

    @Resource
    YwZbglXqMapper ywZbglXqMapper;
    @Resource
    YwZbglGlryMapper ywZbglGlryMapper;
    @Resource
    YwZbglMapper ywZbglMapper;
    @Override
    public void update(List<YwZbglXq> list) {


        YwZbgl ywZbgl=null;
        for (int i = 0; i < list.size(); i++) {
            YwZbglXq xq = list.get(i);
            ywZbgl = ywZbglMapper.selectById(xq.getZbglId());
            ywZbglGlryMapper.sc(xq.getId());
            List<YwZbglGlry> glry = xq.getGlry();
            for (int j = 0; j < glry.size(); j++) {
                YwZbglGlry ywZbglGlry = glry.get(j);
                ywZbglGlry.setId(null);
                ywZbglGlry.setFl(1);
                ywZbglGlry.setGlId(xq.getId());
                ywZbglGlryMapper.insert(ywZbglGlry);
            }
        }
        QueryWrapper<YwZbgl> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("nyz_id",ywZbgl.getNyzId());
        queryWrapper.orderByAsc("id");

        List<YwZbgl> ywZbgls = ywZbglMapper.selectList(queryWrapper);
        Integer iiii=0;
        for (int i = 0; i < ywZbgls.size(); i++) {
            YwZbgl ywZbgl1 = ywZbgls.get(i);
            QueryWrapper<YwZbglXq> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("zbgl_id",ywZbgl1.getId());

            List<YwZbglXq> ywZbglXqs = ywZbglXqMapper.selectList(queryWrapper1);
            for (int j = 0; j < ywZbglXqs.size(); j++) {
                YwZbglXq xq = ywZbglXqs.get(j);
                xq.setPx(iiii);
                ywZbglXqMapper.updateById(xq);
                iiii++;
            }
        }

    }

    @Override
    public List<YwZbglXq> all(YwZbglXq ywZbglXq) {
        QueryWrapper<YwZbglXq> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("zbgl_id",ywZbglXq.getZbglId());
        List<YwZbglXq> ywZbglXqs = ywZbglXqMapper.selectList(queryWrapper);
        for (int i = 0; i < ywZbglXqs.size(); i++) {
            YwZbglXq xq = ywZbglXqs.get(i);
            Map<String,Object> map=new HashMap<>();
            List<YwZbglGlry> all1 = ywZbglGlryMapper.all(xq.getId(), 1,1);
            List<YwZbglGlry> all2 = ywZbglGlryMapper.all(xq.getId(), 2,1);
            List<YwZbglGlry> all3 = ywZbglGlryMapper.all(xq.getId(), 3,1);
            if (all1!=null && all1.size()>0){
                map.put("a1",all1.get(0));
            }else {
                map.put("a1",null);
            }
            if (all2!=null && all2.size()>0){
                map.put("a2",all2.get(0));
            }else {
                map.put("a2",null);
            }
            map.put("a3",all3);
            ywZbglXqs.get(i).setMap(map);
        }
        return ywZbglXqs;
    }

    @Override
    public List<Map<String, Object>> gjrqcx(YwZbglXq ywZbglXq) {
        Integer zhouji = DateTimeUtil.zhouji(ywZbglXq.getSj());

        QueryWrapper<YwZbgl> queryWrapper33=new QueryWrapper<>();
        queryWrapper33.eq("zj",zhouji);
        queryWrapper33.eq("nyz_id",ywZbglXq.getSszdId());
        YwZbgl ywZbgl = ywZbglMapper.selectOne(queryWrapper33);


        QueryWrapper<YwZbglXq> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("zbgl_id",ywZbgl.getId());
        List<YwZbglXq> ywZbglXqs = ywZbglXqMapper.selectList(queryWrapper);
        List<Map<String, Object>> list=new ArrayList<>();
        for (int i = 0; i < ywZbglXqs.size(); i++) {
            YwZbglXq xq = ywZbglXqs.get(i);
            Map<String,Object> map=new HashMap<>();
            List<YwZbglGlry> all1 = ywZbglGlryMapper.all(xq.getId(), 1,1);
            List<YwZbglGlry> all2 = ywZbglGlryMapper.all(xq.getId(), 2,1);
            List<YwZbglGlry> all3 = ywZbglGlryMapper.all(xq.getId(), 3,1);
            List<Long> ids=new ArrayList<>();
            List<String> phones=new ArrayList<>();
            if (all1!=null && all1.size()>0){
                map.put("a1",all1.get(0).getUserName());
                ids.add(all1.get(0).getUserId());
                phones.add(all1.get(0).getUserPhone());
            }else {
                map.put("a1",null);
            }
            if (all2!=null && all2.size()>0){
                map.put("a2",all2.get(0).getUserName());
                ids.add(all2.get(0).getUserId());
                phones.add(all2.get(0).getUserPhone());
            }else {
                map.put("a2",null);
            }
            String aaa="";
            for (int j = 0; j < all3.size(); j++) {
                if (j==0){
                    aaa=all3.get(j).getUserName();
                }else {
                    aaa+=","+all3.get(j).getUserName();
                }
                ids.add(all3.get(0).getUserId());
                phones.add(all3.get(0).getUserPhone());
            }
            map.put("a3",aaa);
            map.put("ids",ids);
            map.put("phones",phones);
            map.put("xqId",xq.getId());
            list.add(map);
        }
        return list;
    }


    @Override
    public List<Map<String, Object>> drry(YwZbglXq ywZbglXq) {
        Integer zhouji = DateTimeUtil.zhouji(ywZbglXq.getSj());

        QueryWrapper<YwZbgl> queryWrapper33=new QueryWrapper<>();
        queryWrapper33.eq("zj",zhouji);
        queryWrapper33.eq("nyz_id",ywZbglXq.getSszdId());
        YwZbgl ywZbgl = ywZbglMapper.selectOne(queryWrapper33);


        QueryWrapper<YwZbglXq> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("zbgl_id",ywZbgl.getId());
        List<YwZbglXq> ywZbglXqs = ywZbglXqMapper.selectList(queryWrapper);
        List<Map<String, Object>> list=new ArrayList<>();
        for (int i = 0; i < ywZbglXqs.size(); i++) {
            YwZbglXq xq = ywZbglXqs.get(i);
            Map<String,Object> map=new HashMap<>();
            List<YwZbglGlry> all1 = ywZbglGlryMapper.all(xq.getId(), 1,1);
            List<YwZbglGlry> all2 = ywZbglGlryMapper.all(xq.getId(), 2,1);
            List<YwZbglGlry> all3 = ywZbglGlryMapper.all(xq.getId(), 3,1);

            map.put("a1",all1.get(0));
            map.put("a2",all2.get(0));
            map.put("a3",all3);
            map.put("xqId",xq.getId());
            list.add(map);
        }
        return list;
    }
}
